﻿using MapleLeaf.Application.CookBook.Parameter;
using MapleLeaf.Core.Const;
using MapleLeaf.Core.Dto;
using MapleLeaf.Core.FriendlyException;
using MapleLeaf.Model.CookBook.Organization.Entity;
using MapleLeaf.Service.CookBook.Menu;
using MapleLeaf.Service.CookBook.Organization;

namespace MapleLeaf.Application.CookBook {
    /// <summary>
    /// 
    /// </summary>
    public class AuthClient : IApplicationClient {

        private readonly SysUserService _sysUserService;
        private readonly SysRoleService _sysRoleService;
        /// <summary>
        /// 
        /// </summary>
        public AuthClient(SysUserService sysUserService, SysRoleService sysRoleService) {
            _sysUserService = sysUserService;
            _sysRoleService = sysRoleService;
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input">登录参数</param>
        /// <returns>Token信息</returns>
        public async Task<UserInfo> Login(LoginParameter input) {

            UserInfo user;
            List<RoleEntity> roles;

            user = await _sysUserService.CheckUserInfoByAccount(input.UserName, input.Password!);
            if (user == null) {
                throw ApiFriendlyException.OhBus(ResponseCodeConst.AccountORPasswordError);
            }
            roles = await _sysRoleService.GetRoleListByUserId(user.Id);

            user.Roles = roles?.ConvertAll(p => p.Code) ?? [];
            user.Device = input.Device;

            return user;
        }
        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public void LogOut() {
            //var redisClent = new RedisClient(15);
            //var redisToken = redisClent.Get(account);
            //if (!redisToken.IsNullOrEmpty()) {
            //    redisClent.Remove(account);
            //}
        }
    }
}
